package com.md.livingwidget.toast;

import android.app.Activity;
import android.content.Context;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;

/**
 * Created by yxt
 *
 * @date 2020/9/20.
 * description：
 */
public class ToastUtil {
    private static ToastUtil instance;
    private final int LENGTH_SHORT = 0x00;
    private final int LENGTH_LONG = 0x01;

    private final int ANIMATION_DURATION = 600;
    private boolean isShowing = false;
    private Context mContext;
    private int HIDE_DELAY = 2000;

    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    isShowing = false;
                    break;
            }
            return false;
        }
    });

    public static ToastUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (ToastUtil.class) {
                if (instance == null) {
                    instance = new ToastUtil();
                    instance.mContext = context;
                }
            }
        }
        return instance;
    }

    // 表示吐司里显示的文字
    public ToastUtil makeText(View view, int HIDE_DELAY) {
       PHToast.toastStack.push(view);
        if (HIDE_DELAY == LENGTH_LONG) {
            this.HIDE_DELAY = 2500;
        } else {
            this.HIDE_DELAY = 1500;
        }
        mHandler.sendEmptyMessageDelayed(1, HIDE_DELAY);
        return this;
    }

    private void wakeUp() {
        if (!PHToast.toastStack.empty() && !isShowing) {
            isShowing = true;
            doshow(PHToast.toastStack.pop());
        }
    }

    private void doshow(View view) {
        try {
            if (mContext instanceof Activity) {
                WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
                WindowManager.LayoutParams params = new WindowManager.LayoutParams();
                int flag = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
                        WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
                }
                else {
                    params.type = WindowManager.LayoutParams.TYPE_PHONE;
                }
                params.flags = flag;
                params.format = PixelFormat.TRANSPARENT; /*透明背景,否则会黑色*/
                params.width = WindowManager.LayoutParams.WRAP_CONTENT;
                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
                params.gravity = Gravity.CENTER_VERTICAL | Gravity.CENTER;
                windowManager.addView(view, params);
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        isShowing = false;
                        windowManager.removeView(view);
                        wakeUp();
                    }
                }, HIDE_DELAY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void show() {
        wakeUp();
    }
}
